#include<stdio.h>
#include <stdlib.h>

typedef enum Status{
    OK,
    ERR
}Status;
typedef struct Node{
    int val;
    struct Node * next;
}Node, * LinkList;

LinkList LinkListCreate(int *a,int n){
    LinkList ans=NULL;
    int i=0;
    if(n>0){
        ans=(LinkList)malloc(sizeof(Node));
        ans->val = a[i];
        LinkList cur=ans,s=NULL;
        ++i;
        while(i<n){
            s=(LinkList)malloc(sizeof(Node));
            s->val = a[i];
            cur->next = s;
            cur = s;
            ++i;
        }
        cur->next =NULL;

    }
    return ans;

}

Status LinkInsert(LinkList *l,int pos,int val){
    if(pos<0) return ERR;
    if(pos==0){
        LinkList s =(LinkList)malloc(sizeof(Node));
        s->val = val;
        s->next = *l;
        *l = s;
        return OK;
    }
    LinkList head = *l;
    int startIndex=0;
    while(head&&startIndex<pos-1){
        head = head->next;
        ++startIndex;
    }
    if(!head) return ERR;
    LinkList s =(LinkList)malloc(sizeof(Node));
    s->val = val;
    s->next = head->next;
    head->next =s;
    return OK;
}

Status printLinkList(LinkList l){
    if(l){
        LinkList head = l;
        while(head){
            printf("%d->",head->val);
            head = head->next;
        }
        return OK;
    }
    return ERR;
}

void destoryLinkList(LinkList l){
    LinkList head=NULL;
    int count =0;
    while(l){
        ++count;
        head = l;
        l = l->next;
        free(head);
        printf("%d ",count);
        head=NULL;
    }

}

int main(){
    int data[]={1,2,4,5,6,7};
    LinkList head = LinkListCreate(data,6);
    printf("创建成功\n");
    int err=OK;
    err = printLinkList(head);
    printf("\n");
    if(err==ERR){
        printf("打印出错\n");
    }
    // 插入新的头节点
    err = LinkInsert(&head,0,0);
    if(err==ERR){
        printf("插入出错\n");
    }
    err = printLinkList(head);
        printf("\n");
    if(err==ERR){
        printf("打印出错\n");
    }
    err = LinkInsert(&head,3,3);
    if(err==ERR){
        printf("插入出错\n");
    }
    err = printLinkList(head);
        printf("\n");
    if(err==ERR){
        printf("打印出错\n");
    }
    destoryLinkList(head);

}